﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FisheryPlatform.Entity;
using FisheryPlatform.Bussiness.ShareCode;

namespace FisheryPlatform.Web.Admin.ShareCode
{
    public partial class CodeAdd : System.Web.UI.Page
    {
        protected int ResultNum = 0;
        protected long ClassId = 0;
        protected long ParentId = 0;
        protected ShareCodeClassEntity ClassInfo = null;
        protected ShareCodeEntity ParentCodeInfo = null;
        protected ShareCodeEntity CodeInfo = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Save();
            }
            InitData();
        }

        protected void InitData()
        {
            #region
            if (!long.TryParse(Request.QueryString["cid"], out ClassId) && ClassId <= 0)
            {
                Response.Write("参数错误");
                Response.End();
            }

            long.TryParse(Request.QueryString["pid"], out ParentId);
            txt_CodeId.Value = Request.QueryString["id"] ?? "0";

            ClassInfo = ShareCodeManager.GetByShareCodeClassId(ClassId);
            if (ParentId > 0)
            {
                ParentCodeInfo = ShareCodeManager.GetShareCodeById(ParentId);
            }
            else
            {
                ParentCodeInfo = new ShareCodeEntity();
            }

            long id = 0;
            long.TryParse(Request.QueryString["id"], out id);
            if (id > 0)
            {
                CodeInfo = ShareCodeManager.GetShareCodeById(id);
                this.txt_Name.Value = CodeInfo.Name;
                txt_NameEN.Value = CodeInfo.NameEn;
            }
            else
            {
                CodeInfo = new ShareCodeEntity();
            }
            #endregion
        }

        private void Save()
        {
            #region
            if (string.IsNullOrEmpty(txt_Name.Value))
            {
                ResultNum = 2;
                return;
            }
            ParentCodeInfo = new ShareCodeEntity();
            ParentCodeInfo.CodeID = long.Parse(txt_CodeId.Value);
            ParentCodeInfo.ClassID = long.Parse(Request.QueryString["cid"]);
            ParentCodeInfo.ParentId = long.Parse(Request.QueryString["pid"]);
            ParentCodeInfo.Name = txt_Name.Value.Trim();
            ParentCodeInfo.NameEn = txt_NameEN.Value.Trim();

            if (ShareCodeManager.SaveShareCode(ParentCodeInfo))
            {
                ResultNum = 1;
            }
            else
            {
                ResultNum = 3;
            }
            #endregion
        }
    }
}